package casa.depesos.app.com.dlg;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import casa.depesos.app.com.adapter.ChooseAdapter;
import casa.depesos.app.com.databinding.FragmentChooseBinding;

public class ChooseDlg extends FullScreenDlg {

    protected FragmentChooseBinding binding;
    protected ChooseAdapter adapter;
    protected List list;
    private String now;
    protected String title;

    public ChooseDlg setData(List list, String now, String title) {
        this.list = list;
        this.now = now;
        this.title = title;

        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentChooseBinding.inflate(inflater, container, false);
        binding.title.setText(title);
        binding.list.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = getAdapter();
        binding.list.setAdapter(adapter);
        binding.cancel.setOnClickListener(view -> dismiss());
        binding.ok.setOnClickListener(view -> dismiss(true, adapter.getCurrent()));

        return binding.getRoot();
    }

    protected ChooseAdapter getAdapter() {
        int j = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).toString().equalsIgnoreCase(now)) {
                j = i;
                break;
            }
        }
        return new ChooseAdapter(list, j);
    }
}
